package com.glasses.business.pay.com;

import android.app.Activity;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;


/**
 * @author wang
 * @version V1.0
 * @Package net.wang.pay
 * @Description: 支付key 获取
 * @date 2016-3-10 上午11:23:24
 */
public class ConstantKeys {

    private static boolean isInitAliPayKeys;
    private static boolean isInitWxPayKeys;
    private static boolean isInitUpPayKeys;

    private static boolean fromXML;

    private static final String TAG = ConstantKeys.class.getName();

    private static final String ALIPAY_PARTNER_ID = "ALIPAY_PARTNER_ID";
    private static final String ALIPAY_SELLER_ID = "ALIPAY_SELLER_ID";
    private static final String ALIPAY_PRIVATE_KEY = "ALIPAY_PRIVATE_KEY";
    private static final String ALIPAY_PUBLIC_KEY = "ALIPAY_PUBLIC_KEY";

    private static final String WXPAY_APP_ID = "WXPAY_APP_ID";
    private static final String WXPAY_MCH_ID = "WXPAY_MCH_ID";
    private static final String WXPAY_API_KEY = "WXPAY_API_KEY";

    private static final String UPPAY_PUBLICKEY_PM_MODULUS = "UPPAY_PUBLICKEY_PM_MODULUS";
    private static final String UPPAY_PUBLIC_EXPONENT = "UPPAY_PUBLIC_EXPONENT";
    private static final String UPPAY_PUBLIC_KEY_PRODUCT_MODULUS = "UPPAY_PUBLIC_KEY_PRODUCT_MODULUS";

    private ConstantKeys() {
    }

    /**
     * 通过 AndroidManifest.xml 配置参数初始化配置参数
     *
     * @return boolean
     * @autour BaoHong.Li
     * @update (date)
     */
    public static boolean initKeys(Activity activity) {
        if (!isInitAliPayKeys && fromXML) {
            initAliPayKeys(getMetaData(activity, ALIPAY_PARTNER_ID),
                    getMetaData(activity, ALIPAY_SELLER_ID),
                    getMetaData(activity, ALIPAY_PRIVATE_KEY),
                    getMetaData(activity, ALIPAY_PUBLIC_KEY));
        }

        if (!isInitWxPayKeys && fromXML) {
            initWxPayKeys(getMetaData(activity, WXPAY_APP_ID),
                    getMetaData(activity, WXPAY_MCH_ID),
                    getMetaData(activity, WXPAY_API_KEY));
        }

        if (!isInitUpPayKeys && fromXML) {
            initUpPayKeys(getMetaData(activity, UPPAY_PUBLICKEY_PM_MODULUS),
                    getMetaData(activity, UPPAY_PUBLIC_EXPONENT),
                    getMetaData(activity, UPPAY_PUBLIC_KEY_PRODUCT_MODULUS));

        }
        return true;
    }

    /**
     * @param @param  activity
     * @param @param  key
     * @param @return 设定文件
     * @return String    返回类型
     * @Title: getMetaData
     * @Description: 在application应用<meta-data>元素。
     */
    public static String getMetaData(Activity activity, String key) {

        ApplicationInfo appInfo = null;
        String value = "";
        try {
            appInfo = activity.getPackageManager().getApplicationInfo(
                    activity.getPackageName(), PackageManager.GET_META_DATA);
            value = String.valueOf(appInfo.metaData.get(key));
            L.d(TAG, "meta-data :key: " + key);
            L.d(TAG, "  meta-data :value: " + value);
        } catch (NameNotFoundException e) {
            L.w(TAG, e.getMessage());
        }

        return value;
    }


    /**
     * @param @param  partnerId
     * @param @param  sellerId
     * @param @param  privateKey
     * @param @param  publicKey
     * @param @return 设定文件
     * @return boolean    返回类型
     * @Title: initAliPayKeys
     * @Description: 初始化 支付宝 配置参数
     */
    public static boolean initAliPayKeys(String partnerId, String sellerId,
                                         String privateKey, String publicKey) {
        AliPay.PARTNER_ID = partnerId;
        AliPay.SELLER_ID = sellerId;
        AliPay.PRIVATE_KEY = privateKey;
        AliPay.ALIPAY_PUBLIC_KEY = publicKey;
        isInitAliPayKeys = true;
        return true;

    }


    /**
     * @param @param  appId
     * @param @param  mchId
     * @param @param  appKey
     * @param @return 设定文件
     * @return boolean    返回类型
     * @Title: initWxPayKeys
     * @Description: 初始化 微信支付 配置参数
     */
    public static boolean initWxPayKeys(String appId, String mchId,
                                        String appKey) {
        WxPay.APP_ID = appId;
        WxPay.MCH_ID = mchId;
        WxPay.API_KEY = appKey;
        isInitWxPayKeys = true;
        return true;

    }

    public static boolean initUpPayKeys(String PublicKeyPMModulus,
                                        String publicExponent, String PublicKeyProductModulus) {
        UpPay.PublicKeyPMModulus = PublicKeyPMModulus;
        UpPay.PublicKeyProductModulus = PublicKeyProductModulus;
        UpPay.publicExponent = publicExponent;
        isInitUpPayKeys = true;
        return true;

    }

    /**
     * alipay keys
     *
     * @version V1.0
     * @update (date)
     */
    public static class AliPay {

        /**
         * 合作者身份 ID parameter "partner" in pay interface
         */
        public static String PARTNER_ID = "2088621562557920";

        /**
         * 企业支付宝帐号 parameter "seller_id" in pay interface
         */
        public static String SELLER_ID = "18610808479@163.com";
        /**
         * 参数编码字符集，目前只支持 utf-8 parameter "_input_charset" in pay interface
         */
        public static String INPUT_CHARSET = "utf-8";

        /**
         * 签名方式，目前只支持 RSA parameter "sign_type" in pay interface
         */
        public static String SIGN_TYPE = "RSA";

        /**
         * 商户私钥 parameter "sign" in pay interface
         */
        public static String PRIVATE_KEY = "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBALEciTanD6/fDb6UMYFGa2rSqof343A+9Zam3qq6SY1DUUZJSwO+7DBR3K1MfJrAmTApkooteKPlpKbHHdqvRrHemn64K6k6ytjH+uJNmE6fFORQE/mx2wyMJc0CHnsfEKT6APlOG8jn+2+hjg1X6I4dHAWrzebnSBp9J7f8XeBRAgMBAAECgYBa6TLlgstikzQS5y6iFOt+YbR3Jp7wNuEI+47AQ+8NO8i0M1r2CUJjayHDMsejB8maFzzJAJ40QuRJV1vTrLS0LczDsXm7+0YsXrtcTduDq3L+o1+tWYMFe9AINPOlcfTqmOxjHR67oNObsPd5prnAJXHV8B6UTX0i4kccxgUpIQJBANpBHgv+ysmQVDzW9MYV5dDYcYyFFUqhIzTNU5Ro2VswMkx4nCJwpoldFJIXygbB5zKichHY2wDPXOCEYxF4uuMCQQDPveEFUrHXEHPTOWU1v8PEoJRxYhQ+67/UeQO1s3ucl1t/wjriO7fk3VT/m+19chc+1uE1WWdfL3/SvrwhfVo7AkBErtlVzQjkBZd8zeiZrap03qhdIERjB/KcW0FCIvzDxZcOmaYI8Ip4D+K5VuMs3Th0pspe8294AcmongDPLr8hAkAOQgf5p41RJ23rp1rM75ZuHl9R0Nt23hNSb+PTSrhHLRQ4looYEOU0LgSxbnBEVdpGk9TtJm+z1ry9KB4dZk8VAkAZ3eDPWqVnacdkf5oMrkyz0oiMxdtWhrD83Fbiwq4UlRPwdN067Hiw7TYlpE21Lr9FC2EShSsrJNbCjJie1vMw";

        /**
         * 支付宝公钥(不是应用公钥)
         */
//        public static String ALIPAY_PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCxHIk2pw+v3w2+lDGBRmtq0qqH9+NwPvWWpt6qukmNQ1FGSUsDvuwwUdytTHyawJkwKZKKLXij5aSmxx3ar0ax3pp+uCupOsrYx/riTZhOnxTkUBP5sdsMjCXNAh57HxCk+gD5ThvI5/tvoY4NV+iOHRwFq83m50gafSe3/F3gUQIDAQAB";
//        public static String ALIPAY_PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCnxj/9qwVfgoUh/y2W89L6BkRAFljhNhgPdyPuBV64bfQNN1PjbCzkIM6qRdKBoLPXmKKMiFYnkd6rAoprih3/PrQEB/VsW8OoM8fxn67UDYuyBTqA23MML9q1+ilIZwBC2AQ2UBVOrFXfFl75p6/B5KsiNG9zpgmLCUYuLkxpLQIDAQAB";
        public static String ALIPAY_PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDDI6d306Q8fIfCOaTXyiUeJHkrIvYISRcc73s3vF1ZT7XN8RNPwJxo8pWaJMmvyTn9N4HQ632qJBVHf8sxHi/fEsraprwCtzvzQETrNRwVxLO5jVmRGi60j8Ue1efIlzPXV9je9mkjzOmdssymZkh2QhUrCmZYI/FCEa3/cNMW0QIDAQAB";
    }

    /**
     * wx pay keys
     *
     * @version V1.0
     * @update (date)
     */
    public static class WxPay {
        /**
         * appid 请同时修改 androidmanifest.xml里面，.PayActivityd里的属性
         * <p>
         * <b><i> data android:scheme="wxb4ba3c02aa476ea1"<i><b> 为新设置的appid
         */
        public static String APP_ID = "wxbec1a8b64af6df72";
        public static String APP_SECRET = "bcd0c0340e21a781e8bd854f7c130330";
        /***
         * 商户号
         */
        public static String MCH_ID = "1460767502";

        /***
         * API密钥，在商户平台设置
         */
        public static String API_KEY = "xiaosufu1985xiaosufu1985xiaosufu";

    }

    /**
     * UpPay keys
     * <p>
     * PublicKeyPMModulus 换成PM环境商户验签的公钥模数
     * </p>
     * <p>
     * PublicKeyProductModulus 换成生产环境商户验签的公钥模数
     * </p>
     * <p>
     * publicExponent 默认值：　65537
     * </p>
     */


    public static class UpPay {
        public static String PublicKeyPMModulus = "";
        public static String publicExponent = "65537";
        public static String PublicKeyProductModulus = "";

    }

}
